<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="单据编号" prop="documentCode">
        <el-input
          v-model="queryParams.documentCode"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据名称" prop="documentName">
        <el-input
          v-model="queryParams.documentName"
          placeholder="请输入单据名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型">
          <el-option
            v-for="dict in dict.type.tool_history_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据日期" prop="params">
        <el-date-picker
          clearable
          v-model="queryParams.params"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          placeholder="请选择归还日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工器具编号" prop="toolCode">
        <el-input
          v-model="queryParams.toolCode"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工器具名称" prop="toolName">
        <el-input
          v-model="queryParams.toolName"
          placeholder="请输入单据名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>

    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

    <el-table border v-loading="loading" :data="historyList">
      <el-table-column label="单据编号" align="center" prop="documentCode">
<!--        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['tm:history:query']"
          >{{ scope.row.documentCode }}
          </el-button>
        </template>-->
      </el-table-column>
      <el-table-column label="单据名称" align="center" prop="documentName" />
      <el-table-column label="单据日期" align="center" prop="documentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.documentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tool_history_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="工器具编号" width="120px" align="center" prop="toolCode" />
      <el-table-column label="工器具名称" width="120px" align="center" prop="toolName" />
      <el-table-column label="型号" align="center" prop="spec" />
      <el-table-column label="品牌" align="center" prop="brand"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

  </div>
</template>

<script>
import { listHistory, getHistory } from "@/api/mes/tm/history";

export default {
  name: "Return",
  dicts: ['tool_history_type'],
  data() {
    return {
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 归还表格数据
      historyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        documentCode: null,
        documentName: null,
        params: [],
        type:null,
        toolCode:null,
        toolName:null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询历史记录列表 */
    getList() {
      this.loading = true;
      const query = { ...this.queryParams };
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      console.log(query);
      listHistory(query).then(response => {
        console.log(response);
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        documentId: null,
        documentCode: null,
        documentName: null,
        documentDate: new Date(),
        toolId: null,
        toolCode: null,
        toolName: this.returnName,
        type: null,
        spec:null,
        brand:null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('tm/history/export', {
        ...this.queryParams
      }, `return_${new Date().getTime()}.xlsx`)
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      getHistory(row.documentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看历史记录";
        this.optType = "view";
      });
    },


  }
};
</script>
